package com.subfg.payment.controller.noauth;


import com.alibaba.fastjson.JSONObject;
import com.stripe.model.Event;
import com.stripe.model.PaymentIntent;
import com.stripe.model.StripeObject;
import com.stripe.net.ApiResource;
import com.subfg.common.entity.call.request.RequestPayModel;
import com.subfg.common.rabbitmq.constant.ExchangeApi;
import com.subfg.common.rabbitmq.constant.QueueContentModel;
import com.subfg.common.rabbitmq.produer.MQSender;
import com.subfg.common.util.auth.GenerateId;
import com.subfg.common.util.common.BigDecimalUtil;
import com.subfg.common.util.common.TimeUtil;
import com.subfg.payment.constant.AliTradeStatusEnum;
import com.subfg.payment.constant.StripeTradeEnum;
import com.subfg.payment.entity.callback.AliCallBackModel;
import com.subfg.common.rabbitmq.constant.MQSubsetModel;
import com.subfg.payment.rabiitmq.constant.QueueApi;
import com.subfg.payment.rabiitmq.constant.RoutingKeyApi;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("/callBack")
public class PaymentNotifyCall {
    private Logger log = LoggerFactory.getLogger(PaymentNotifyCall.class);

    @Autowired
    private MQSender mqSender;
    @Autowired
    private GenerateId generateId;

    @ApiOperation(value = "支付宝支付成功后回调地址")
    @RequestMapping(value = "/alipayNotify", method = {RequestMethod.POST})
    @ResponseBody
    public String alipayNotify(AliCallBackModel aliCallBackModel) {
        Long currentTime = TimeUtil.getCurrentTime();
        log.info("支付宝回调成功, 回调数据如下: " + aliCallBackModel.getTrade_no());
        // 判断支付状态为成功
        if (aliCallBackModel.getTrade_status().equals(AliTradeStatusEnum.SUCCESS.getStatus()) && aliCallBackModel.getRefund_fee() == null) {
            RequestPayModel requestPayModel = JSONObject.parseObject(aliCallBackModel.getBody(), RequestPayModel.class);
            String mqId = generateId.genrateMqId();
            requestPayModel.setChannelNo(aliCallBackModel.getTrade_no());
            requestPayModel.setActualAmount(aliCallBackModel.getReceipt_amount());
            requestPayModel.setCallBackTime(currentTime);
            mqSender.sendMQMessage(ExchangeApi.EXCHANGE_COMMON, RoutingKeyApi.PAYMENT_CALLBACK,
                    new QueueContentModel(mqId, 2, 0, requestPayModel));
        }
        return "success";
    }

    @ApiOperation(value = "stripe 支付成功后的回调地址")
    @RequestMapping(value = "/stripeNotify", method = {RequestMethod.POST})
    @ResponseBody
    public String stripeNotify(@RequestBody String notify) {
        log.info("stripe 支付成功, 回调数据");
        Long currentTime = TimeUtil.getCurrentTime();
        String mqId = generateId.genrateMqId();
        Event event = ApiResource.GSON.fromJson(notify, Event.class);
        StripeObject stripeObject = event.getData().getObject();
        // 需要判断Stripe 的支付状态必须为支付成功状态
        if (event.getType().equals(StripeTradeEnum.SUCCESSDED.getStatus())) {
            PaymentIntent paymentIntent = (PaymentIntent) stripeObject;
            // RequestPayModel requestPayModel = JSONObject.parseObject(aliCallBackModel.getBody(), RequestPayModel.class);
            BigDecimal actualAmount = BigDecimalUtil.divideBigDeimalHoured(paymentIntent.getAmount());
            MQSubsetModel MQSubsetModel = JSONObject.parseObject(paymentIntent.getMetadata().get("attach"), MQSubsetModel.class);
            MQSubsetModel.setCallBackTime(currentTime);
            mqSender.sendMQMessage(ExchangeApi.EXCHANGE_COMMON, QueueApi.PAYMENT_QUEUE_PAYMENT_COMMON,
                    new QueueContentModel(mqId, 2, 0, MQSubsetModel));
        }
        return "success";
    }


    @ApiOperation(value = "stripe 退款回调地址")
    @RequestMapping(value = "stripeRefundNotify", method = {RequestMethod.POST})
    @ResponseBody
    public String stripeRefundNotify(@RequestBody String notify) {

        return "success";
    }
}
